import requests
import re


def test_1():
    r = requests.get('https://www.baidu.com/')
    r.encoding = 'utf-8'
    print(type(r))
    print(r.status_code)
    print(type(r.text))
    print(r.text)
    print(r.cookies)


class Person(object):
    name = "jzq"

    def get_name(self):
        return self.name


def test_2():
    jzq = Person()
    jzq.name = "hahaha"
    print(jzq.get_name())


def test_3():
    # r = requests.get('http://httpbin.org/get')
    params = {
        'name': 'jzq',
        'age': '23'
    }
    r = requests.get('http://httpbin.org/get', params)
    print(r.json())


def test_4():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
    }
    r = requests.get("https://www.zhihu.com/explore", headers=headers)
    r.encoding = 'utf-8'
    pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>', re.S)
    titles = re.findall(pattern, r.text)
    print(titles)


def test_5():
    r = requests.get("https://github.com/favicon.ico")
    with open('favicon.ico', 'wb') as f:
        f.write(r.content)


def test_6():
    data = {'name': 'germey', 'age': '22'}
    r = requests.post("http://httpbin.org/post", data=data)
    print(r.text)


if __name__ == '__main__':
    test_6()
